{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn \n",
    "from torch.nn import functional as F\n",
    "from torchviz import make_dot\n",
    "from tensorboardX import SummaryWriter\n",
    "from spair.models import SPAIR\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-81afc3e5c216>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m32\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m128\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m128\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mdevice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cpu\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mdt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoday\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstrftime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'%b-%d'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mwriter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSummaryWriter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'logs_v2/%s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mdt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'torch' is not defined"
     ],
     "ename": "NameError",
     "evalue": "name 'torch' is not defined",
     "output_type": "error"
    }
   ],
   "source": [
    "\n",
    "img = torch.rand((32,1,128,128))\n",
    "device = torch.device(\"cpu\")\n",
    "dt = datetime.today().strftime('%b-%d')\n",
    "writer = SummaryWriter('logs_v2/%s' % dt)\n",
    "\n",
    "spair = SPAIR((1, 128,128),writer,device,)\n",
    "\n",
    "spair_params = dict(spair.named_parameters())\n",
    "decoder_in_weights = spair_params['object_decoder.dense0.weight']\n",
    "encoder_out_param = spair_params['object_encoder.out.weight']\n",
    "print('making dot')\n",
    "# make_dot(spair(img))\n",
    "\n",
    "\n",
    "# make_dot(spair(img), params=dict(spair.named_parameters()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[1.6250, 1.6250, 1.6250, 1.6250],\n        [1.6250, 1.6250, 1.6250, 1.6250],\n        [0.8750, 0.8750, 0.8750, 0.8750],\n        [0.8750, 0.8750, 0.8750, 0.8750]])"
     },
     "metadata": {},
     "output_type": "execute_result",
     "execution_count": 92
    }
   ],
   "source": [
    "\n",
    "class test_grad_net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.output = nn.Linear(4, 4)\n",
    "        self.output.weight.data.fill_(1)\n",
    "        self.output.bias.data.fill_(0)\n",
    "    def forward(self, x):\n",
    "        return self.output(x)\n",
    "        \n",
    "net = test_grad_net()\n",
    "\n",
    "x = torch.ones(2, 8, dtype=torch.float, requires_grad=False) * 0.1# x1 and x2 batch, batch size = 2\n",
    "y = torch.tensor([[1,.1,.1,.1,.1,.1,1,.1],[.1,1,.1,.1,.1,.1,.1,1]], dtype=torch.float, requires_grad=False)\n",
    "\n",
    "x1 = x[:, :4]\n",
    "x2 = x[:, 4:] * 2\n",
    "y1 = y[:, :4]\n",
    "y2 = y[:, 4:]\n",
    "out1 = net(x1)\n",
    "out2 = net(x2)\n",
    "\n",
    "out = torch.cat([out1, out2], dim=-1)\n",
    "\n",
    "loss = F.binary_cross_entropy(out, y, reduction='sum')\n",
    "\n",
    "loss.backward()\n",
    "\n",
    "W = net.output.weight\n",
    "W.grad\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}